import Vue from "vue";
import { arraytoLowerCase } from "@/core/util/util";

const api = window.$api.data.dbsource;
const interfaceApi = window.$api.data.dataInterface;

//console.log(interfaceApi);
const dataSource = {
  namespaced: true,
  state: {
    list: [],
    isUpdate: false,
    dataApi: {},
    dataPick: {},
    dataGroup: {},
    apiDataGroup: {}, //api接口options缓存数据
    apiDataByParamterGroup: {},
    colNameGroup: {},
    dataByParamterGroup: {},

    updateStateList: {},
    updateStateListForCol: {},
    updateStateListForParam: {},
  },
  actions: {
    async loadList({ commit, state }, isCache) {
      if (!state.isUpdate || !isCache) {
        state.isUpdate = true;
        const res = await api.getList();
        const data = res.data.data;
        commit("SET_DATASOURCE_LIST", data);
      }
    },
    async loadDataCol({ commit, state }, { code, isCache }) {
      if (!state.updateStateListForCol[code] || !isCache) {
        state.updateStateListForCol[code] = true;
        const res = await api.getColNameList(code);
        const data = res.data.data;
        commit("SET_DATASOURCE_COLNAMEGROUP", { code, data });
      }
    },

    //接口模式-数据源-接口列表
    async apiListData({ commit }, pageData) {
      let params = pageData ? pageData : { rows: 20, page: 1 };
      const res = await interfaceApi.getPage({ ...params, f_EnabledMark: 1 });
      var data = res.data.data.rows;
      commit("SET_API_DATAGROUP", { data });
      return { rows: data, records: res.data.data.records };
    },

    async loadApiDataList({ commit, state }, { apiCode, isCache }) {
      if (!state.updateStateList[apiCode] || !isCache) {
        state.updateStateList[apiCode] = true;
        const apiGet = await window.$awaitWraper(api.apiParams(`${window.$apiUrl}data/datainterface/${apiCode}`));
        const res = await api.apiParams(apiGet.f_Url, "get");
        const data = arraytoLowerCase(res.data.data);
        commit("SET_APIDATASOURCE_DATAGROUP", { apiCode, data });
      }
    },

    async loadApiDataListByParamter({ commit, state }, { apiCode, paramName, paramValue, isCache }) {
      const codeParam = `${apiCode}_${paramValue}`;
      if (!state.updateStateListForParam[codeParam] || !isCache) {
        state.updateStateListForParam[codeParam] = true;
        const apiGet = await window.$awaitWraper(api.apiParams(`${window.$apiUrl}data/datainterface/${apiCode}`));
        const res = await api.apiParams(apiGet.f_Url, "get", { [paramName]: decodeURIComponent(paramValue) });
        const data = arraytoLowerCase(res.data.data);
        //console.log(data);
        commit("SET_APIDATASOURCE_DATABYPARAMTERGROUP", { codeParam, data });
      }
    },

    async loadDataList({ commit, state }, { code, isCache }) {
      if (!state.updateStateList[code] || !isCache) {
        state.updateStateList[code] = true;
        const res = await api.getDataList(code);
        const data = res.data.data;
        commit("SET_DATASOURCE_DATAGROUP", { code, data });
      }
    },

    async loadDataListByParamter({ commit, state }, { code, param, isCache }) {
      const codeParam = `${code}_${param}`;
      if (!state.updateStateListForParam[codeParam] || !isCache) {
        state.updateStateListForParam[codeParam] = true;
        const res = await api.getDataListByParamter(code, param);
        const data = res.data.data;
        //console.log(data);
        commit("SET_DATASOURCE_DATABYPARAMTERGROUP", { codeParam, data });
      }
    },
  },
  mutations: {
    SET_DATASOURCE_LIST: (state, payload) => {
      state.list = payload;
    },
    SET_API_DATAGROUP: (state, payload) => {
      Vue.set(state.dataApi, "api", payload.data);
    },
    SET_APIPICK_DATAGROUP: (state, payload) => {
      Vue.set(state.dataPick, "pick", payload.data);
    },
    SET_APIDATASOURCE_DATAGROUP: (state, payload) => {
      Vue.set(state.apiDataGroup, payload.apiCode, payload.data);
    },
    SET_APIDATASOURCE_DATABYPARAMTERGROUP: (state, payload) => {
      Vue.set(state.apiDataByParamterGroup, payload.codeParam, payload.data);
    },
    SET_DATASOURCE_DATAGROUP: (state, payload) => {
      Vue.set(state.dataGroup, payload.code, payload.data);
    },
    SET_DATASOURCE_COLNAMEGROUP: (state, payload) => {
      Vue.set(state.colNameGroup, payload.code, payload.data);
    },
    SET_DATASOURCE_DATABYPARAMTERGROUP: (state, payload) => {
      Vue.set(state.dataByParamterGroup, payload.codeParam, payload.data);
    },
  },
};

export default dataSource;
